package thread.tongbu;

 class Callme {
      void call(String msg){
        try {
            System.out.print("[" + msg);
            System.out.println("]");
            Thread.sleep(1000);
        }catch (InterruptedException e){
            System.out.println("Interrupted");
        }
    }
}

class Caller implements  Runnable{
     String msg;
     Callme target;
     Thread t;

     public Caller(Callme targ,String s){
         this.target=targ;
         this.msg=s;
         t=new Thread(this);
         t.start();
     }

    @Override
    public void run() {
         synchronized (target) {
             target.call(msg);
         }
    }
}

class Synch{
     public static void main(String[] args){
         Callme target=new Callme();
         Caller ob1=new Caller(target,"Hello");
         Caller ob2=new Caller(target,"Synchronized");
         Caller ob3=new Caller(target,"World");

         //waiting for threads to end
         try {
             ob1.t.join();
             ob2.t.join();
             ob3.t.join();
         }catch (InterruptedException e){
             System.out.println("Interrupted");
         }
     }

}
